package com.myacg.editor.common.unit.panel.base;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.html.HTMLEditorKit;
import com.myacg.editor.common.unit.html.CssBlock;
import com.myacg.editor.common.unit.html.ElementBlock;
import com.myacg.editor.common.unit.html.ElementBlockUtils;
import com.myacg.editor.common.unit.html.HtmlPage;

public class MyHtmlPanel extends JTextPane{
	private HtmlPage htmlPage ;
	private List<ElementBlock>  elements ;
	private List<CssBlock>     styles;
	public MyHtmlPanel(){
		htmlPage = new HtmlPage();
		elements = new ArrayList<>();
		styles = new ArrayList<>();
		this.setEditorKit(new HTMLEditorKit());
		this.setDocument(htmlPage.getDocument());
		this.setEditable(false);
	}
	public HtmlPage getHtmlPage() {
		return htmlPage;
	}
	public void setHtmlPage(HtmlPage htmlPage) {
		this.htmlPage = htmlPage;
	}
	
	/**
	 * 在body末尾追加入一个元素
	 * @param elementBlock
	 */
	public MyHtmlPanel appendElementBlockToBody(ElementBlock elementBlock){
		this.elements.add(elementBlock);
		//通知添加element节点
		htmlPage.appentElementToBody(elementBlock.getInnerHtml());
		return this;
	}
	
	 
	public MyHtmlPanel removeElementBlockById(String id){
		ElementBlock elementBlock = ElementBlockUtils.findElementBlockByid(this.elements, id);
		if(elementBlock!= null ){
			this.elements.remove(elementBlock);
		}
		//通知文档树删除节点
		this.htmlPage.getDocument().removeElement(this.htmlPage.getDocument().getElement(id));
		return this;
	}
	
	/**
	 * 重置htmlbody
	 */
	public void resetHTMLBody(){
		try {
			htmlPage.getDocument().setInnerHTML(htmlPage.getBody(), this.getAllHtmlStr());
		} catch (BadLocationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
	/**
	 * 添加一个样式
	 * @param elementBlock
	 */
	public MyHtmlPanel appendCssBlock(CssBlock cssBlock){
		 this.removeCssBody(cssBlock.getName());
		 this.styles.add(cssBlock);
		 htmlPage.getDocument().getStyleSheet().addRule(cssBlock.print());
		 return this;
	}
	/**
	 * 删除一个样式
	 * @param cssName
	 */
	public MyHtmlPanel removeCssBody(String cssName){
		CssBlock  findCss = ElementBlockUtils.findCssBlockByName(this.styles, cssName);
		if(findCss!=null){
			this.styles.remove(findCss);
			htmlPage.getDocument().getStyleSheet().removeStyle(cssName);
		}
		return this;
	}
	/**
	 * 重置css样式
	 */
	public void resetCsss(){
		for (CssBlock cssBlock : styles) {
			htmlPage.getDocument().getStyleSheet().removeStyle(cssBlock.getName());
		}
		htmlPage.getDocument().getStyleSheet().addRule(this.getAllCssStr());
	}
	
	
	/**
	 * 将目前所有html节点按顺序输出 
	 * @return
	 */
	public String getAllHtmlStr(){
		StringBuffer allstr = new StringBuffer();
		for (ElementBlock elementBlock : elements) {
			allstr.append(elementBlock.getInnerHtml());
		}
		return allstr.toString();
	}
	/**
	 * 获取目前为止的css样式
	 * @return
	 */
	public String getAllCssStr(){
		StringBuffer allstr = new StringBuffer();
		for (CssBlock cssblock:styles) {
			allstr.append(cssblock.print());
		}
		return allstr.toString();
	}
	
}
